<?php
/*
@author: wjf
@contact: Hurrican
@software: PhpStorm
@create: 2022/8/8 13:35
*/
declare(strict_types=1);
namespace app\store\service\agent;

use app\common\model\user\LoginLog;
use app\common\service\BaseService;
use app\store\model\agent\Rel;
use app\store\model\User;

class Player extends BaseService
{
    public function getList($params):array
    {
        $condition = [];
        if(!empty($params['promoter_user_id'])){
            $condition[] = ['fw_agent_rel.promoter_user_id','=',$params['promoter_user_id']];
        }
        if(!empty($params['promoter_nick_name'])){
            //反查user_id
            $promoter_user_ids = User::where('nick_name','like',"%{$params['promoter_nick_name']}%")->column('user_id');
            $condition[] = ['fw_agent_rel.promoter_user_id','IN',$promoter_user_ids];
        }
        $playerWhere = [];
        if(!empty($params['player_user_id'])){
            $playerWhere[] = ['fw_agent_rel.user_id','=',$params['player_user_id']];
        }
        if(!empty($params['player_nick_name'])){
            //反查user_id
            $player_user_ids = User::where('nick_name','like',"%{$params['player_nick_name']}%")->column('user_id');
            $playerWhere[] = ['fw_agent_rel.user_id','IN',$player_user_ids];
        }

        return Rel::with(['playerUser' => function($query){
            $query->field('user_id,mobile,nick_name,created_at');
        }])
            ->where('fw_agent_rel.is_del',0)
            ->join('fw_agent_promoter','fw_agent_promoter.user_id = fw_agent_rel.promoter_user_id','left')
            ->hasWhere('playerUser',$playerWhere)
            ->where($condition)
            ->paginate($params['limit'] ?? 15)
            ->each(function (&$item){
                $user_info = User::detail($item['promoter_user_id']);
                $item['playerUser']['last_login_time'] = '';
                $item['promoterUser'] = [
                        'user_id' =>  $item['promoter_user_id'],
						'mobile' =>  $user_info['mobile'],
						'nick_name' =>  $user_info['nick_name'],
                        'last_login_time' => '',
                        'created_at' =>  $user_info['created_at'],
                ];
            })
            ->toArray();
    }


}